<template>
	<view class="selfAddress">
		<view class="headTop">
			<view class="title">
				地址薄
			</view>
			<navigator class="addressBtn" url="/pages/address/addEidt">
				+ 添加地址
			</navigator>
		</view>
		<view class="list">
			<view class="item" v-for="item in addressList " :key="item._id" @longpress="goUpdate(item._id)" @click="selectAddress(item)">
				<view class="head">
					<view class="user">
						{{item.username}}，{{item.mobile}}
					</view>
					<view class="select">
						<u-button v-if="item.selected" color="#ff5521" plain size="mini" text="默认地址"></u-button>
						<u-button v-else color="#666"  plain  size="mini" text="设为默认" @click="changDefault(item._id)"></u-button>
					</view>
				</view>
				<view class="more">
					{{item.area_name + item.address}}
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	const addressCloudObj = uniCloud.importObject("wanshop-mall-address")
	export default {
		data() {
			return {
				addressList:[]
			};
		},
		onShow(){
			this.getAddressList()
		},
		methods:{
			async getAddressList(){
				let res = await addressCloudObj.getList();
				console.log(res);
				this.addressList = res.data
			},
			async changDefault(id){
				let res = await addressCloudObj.setDefault(id);
				uni.showToast({
					title:"修改成功",
					mask:true
				})
				setTimeout(()=>{
					this.getAddressList();
				},500)
				
				
				// console.log(res);
			},
			goUpdate(id){
				uni.navigateTo({
					url: './addEidt?id='+id,
				});
			},
			selectAddress(item){
				let {address,area_name,username,mobile} = item;
				let deliveryInfo = {
					address:area_name + address,
					username,
					mobile
				}
				uni.$emit("selectAddressEvent",deliveryInfo)
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: $page-bg-color;
	}
	.selfAddress{
		padding: 30rpx;
		.headTop{
			@include flex-box();
			font-size: 30rpx;
			font-weight: bold;
			.addressBtn{
				border:1rpx solid #000;
				border-radius: 50rpx;
				padding:6rpx 15rpx;
			}
		}
		.list{
			padding-top: 30rpx;
			.item{
				background: #fff;
				padding: 40rpx 20rpx;
				margin-bottom: 30rpx;
				border-radius: 15rpx;
				.head{
					@include flex-box();
					font-weight: bold;
				}
				.more{
					font-size: 28rpx;
					color:$text-font-color-2;
				}
			}
		}
	}
</style>
